package com.example.letgo.Fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.letgo.R;
import com.example.letgo.global.Constant;
import com.example.letgo.model.User;
import com.example.letgo.personcenter.AboutUs;
import com.example.letgo.personcenter.OpinionActivity;
import com.example.letgo.personcenter.PersonInfoActivity;
import com.example.letgo.story.StoryDetailsHadActivity;
import com.example.letgo.story.StoryReleaseActivity;
import com.example.letgo.tool.CircleTransform;
import com.example.letgo.trip.TripDetailsHadActivity;
import com.example.letgo.trip.TripReleaseActivity;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.squareup.picasso.Picasso;

import net.cachapa.expandablelayout.ExpandableLinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;

import static io.rong.imlib.statistics.Statistics.TAG;

/**
 * Created by cc on 2018/2/3.
 */

public class PersoncenterFragment extends Fragment {
    private static PersoncenterFragment instance = null;
    @BindView(R.id.my_trip)
    LinearLayout myTrip;
    @BindView(R.id.trip_release)
    LinearLayout tripRelease;
    @BindView(R.id.trip_had)
    LinearLayout tripHad;
    @BindView(R.id.my_story)
    LinearLayout myStory;
    @BindView(R.id.story_shear)
    LinearLayout storyShear;
    @BindView(R.id.story_read)
    LinearLayout storyRead;
    @BindView(R.id.person_info)
    LinearLayout personInfo;
    @BindView(R.id.about_us)
    LinearLayout aboutUs;
    @BindView(R.id.opinion)
    LinearLayout opinion;
    Unbinder unbinder;

    public PersoncenterFragment() {
        // Required empty public constructor
    }

    /**
     * IM需要的返回实例化的对象-hbl
     *
     * @return
     */
    public static PersoncenterFragment getInstance() {
        if (instance == null) {
            instance = new PersoncenterFragment();
        }
        return instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_personcenter, container, false);
        LinearLayout linearLayout = view.findViewById(R.id.p_bg);
        linearLayout.getBackground().setAlpha(150);
        //获取个人信息
        getUserInfo(view);
        //设置展开和收缩的列表
        getExpand(view);
        unbinder = ButterKnife.bind(this, view);
        Log.e("ID",">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>    "+Constant.id);
        return view;
    }

    private void getExpand(View view) {
        LinearLayout L_my_trip = view.findViewById(R.id.my_trip);
        LinearLayout L_my_story = view.findViewById(R.id.my_story);

        final ExpandableLinearLayout ev_trip = view.findViewById(R.id.expandable_layout);
        final ExpandableLinearLayout ev_story = view.findViewById(R.id.expandable_layout2);

        final ImageView imageView_you_1 = view.findViewById(R.id.I_you_1);
        final ImageView imageView_you_2 = view.findViewById(R.id.I_you_2);
        L_my_trip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ev_trip.toggle();
                if (ev_trip.isExpanded()) {
                    imageView_you_1.setImageResource(R.mipmap.you2);
                } else {
                    imageView_you_1.setImageResource(R.mipmap.you);
                }
            }
        });
        L_my_story.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ev_story.toggle();
                if (ev_story.isExpanded()) {
                    imageView_you_2.setImageResource(R.mipmap.you2);
                } else {
                    imageView_you_2.setImageResource(R.mipmap.you);
                }
            }
        });
    }

    private void getUserInfo(View view) {
        int userId = Constant.id;
        final ImageView I_head;
        final TextView t_name, t_sign, t_zuji;
        t_name = view.findViewById(R.id.p_name);
        t_sign = view.findViewById(R.id.p_sign);
        t_zuji = view.findViewById(R.id.p_zuji);
        I_head = view.findViewById(R.id.p_head);
        Log.e("id", String.valueOf(userId));
        /**
         * 获取用户信息，传入登陆时候的userId
         */
        OkGo.get("http://119.29.113.114:8080/gogogo/user/"+userId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.e(TAG, "onSuccess: "+s);
                        Gson gson = new Gson();
                        User user = gson.fromJson(s, User.class);
                        Log.e("Username",user.getUserNickName());
                        if (TextUtils.isEmpty(user.getUserImg())) {
                            Picasso
                                    .with(getActivity())
                                    .cancelRequest(I_head);
                            I_head.setImageDrawable(getResources().getDrawable(R.drawable.head));//当图片为空时显示
                        } else {
                            //图片加载
                            Picasso.with(getActivity()).load("http://119.29.113.114:8080/gogogo/"+user.getUserImg())
                                    .transform(new CircleTransform())
                                    .into(I_head);
                        }

                        t_name.setText(user.getUserNickName());
                        t_sign.setText(user.getUserSign());

                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        Log.e(TAG, "onError: ",e );
                    }
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.trip_release, R.id.trip_had, R.id.story_shear, R.id.story_read, R.id.person_info, R.id.about_us, R.id.opinion})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.trip_release:
                Intent intent = new Intent(getActivity(), TripReleaseActivity.class);
                startActivity(intent);
                break;
            case R.id.trip_had:
                Intent intent5 = new Intent(getActivity(), TripDetailsHadActivity.class);
                startActivity(intent5);
                break;
            case R.id.story_shear:
                Intent intent1 = new Intent(getActivity(), StoryReleaseActivity.class);
                startActivity(intent1);
                break;
            case R.id.story_read:
                Intent intent6 = new Intent(getActivity(), StoryDetailsHadActivity.class);
                startActivity(intent6);
                break;
            case R.id.person_info:
                Intent intent2 = new Intent(getActivity(), PersonInfoActivity.class);
                startActivity(intent2);
                break;
            case R.id.about_us:
                Intent intent3 = new Intent(getActivity(), AboutUs.class);
                startActivity(intent3);
                break;
            case R.id.opinion:
                Intent intent4 = new Intent(getActivity(), OpinionActivity.class);
                startActivity(intent4);
                break;
        }
    }
}
